#include "stdafx.h"
#include "InputGage.h"
#include "RectStatic.h"
#include "iniread.h"
#include "PFMEngine.h"

InputGage::InputGage()
:m_nPosX(0)
,m_nPosY(0)
,m_nMargin(0)
,m_nCurGage(100)
,m_nMaxGage(100)
,m_bCheck(FALSE)
{
}

InputGage::~InputGage()
{
	Release();
}


void InputGage::Release()
{
	for(int i = 0; i < int(m_arrayRectHighlight.size()); ++i)
	{
		SAFE_DELETE(m_arrayRectBlack[i]);		
	}

	for(int i = 0; i < int(m_arrayRectHighlight.size()); ++i)
	{
		SAFE_DELETE(m_arrayRectHighlight[i]);
	}

	for(int i = 0; i < int(m_arrayRectNormal.size()); ++i)
	{
		SAFE_DELETE(m_arrayRectNormal[i]);
	}
}


void InputGage::Init(int nPosX, int nPosY, int nMargin)
{
	Release();

	char szResourceName[1024] = {0,};
	char szKeyName[1024] = {0,};
	int nCurPosX = nPosX;
	int nCurPosY = nPosY;
	m_nImageWidth = GetIniReader().GetIniKeyValue_Int(INI_TYPE_COMMON, "INPUT_RESOURCE", "Timer_Image_Width");
	int nImageCount = GetIniReader().GetIniKeyValue_Int(INI_TYPE_COMMON, "INPUT_RESOURCE", "Timer_Gage_Count");
	for(int i = 0; i < nImageCount; ++i)
	{
		RectStatic* pRect = new RectStatic;
		GetIniReader().GetIniKeyValue_String(INI_TYPE_COMMON, szResourceName, 1024, "INPUT_RESOURCE", "Timer_Black_Img");
		pRect->Create(GetPFMEngine().CreateRect(szResourceName),nCurPosX, nCurPosY, m_nImageWidth, m_nImageWidth);
		m_arrayRectBlack.push_back(pRect);

		pRect = new RectStatic;
		sprintf(szKeyName, "Timer_Normal_%d_Img", i);
		GetIniReader().GetIniKeyValue_String(INI_TYPE_COMMON, szResourceName, 1024, "INPUT_RESOURCE", szKeyName);
		pRect->Create(GetPFMEngine().CreateRect(szResourceName),nCurPosX, nCurPosY, m_nImageWidth, m_nImageWidth);
		m_arrayRectNormal.push_back(pRect);

		pRect = new RectStatic;
		sprintf(szKeyName, "Timer_Highlight_%d_Img", i);
		GetIniReader().GetIniKeyValue_String(INI_TYPE_COMMON, szResourceName, 1024, "INPUT_RESOURCE", szKeyName);
		pRect->Create(GetPFMEngine().CreateRect(szResourceName),nCurPosX, nCurPosY, m_nImageWidth, m_nImageWidth);
		m_arrayRectHighlight.push_back(pRect);

		nCurPosX += m_nImageWidth + nMargin;
	}
}

void InputGage::Update()
{
	m_nCurGage = max(m_nCurGage - 1, 0);

	int nImageCount = int(m_arrayRectBlack.size());
	int nSpace = m_nMaxGage / 5;
	int nCurIndex = m_nCurGage / nSpace;
	for(int i = 0; i < nCurIndex; ++i)
	{
		m_arrayRectNormal[i]->Update();
	}

	if( m_nCurGage > 0 )
	{
		if( m_nCurGage % nSpace < nSpace / 5)
		{
			if(m_bCheck)	m_arrayRectNormal[nCurIndex]->Update();
			else			m_arrayRectHighlight[nCurIndex]->Update();
			m_bCheck = !m_bCheck;
		}
		else
		{
			m_arrayRectNormal[nCurIndex]->Update();
		}
	}
	else
	{
		m_arrayRectBlack[nCurIndex]->Update();	
	}

	for(int i = nCurIndex + 1; i < nImageCount; ++i)
	{
		m_arrayRectBlack[i]->Update();
	}
}

void InputGage::SetPos(int nPosX, int nPosY, int nMargin)
{
	int nImageCount = int(m_arrayRectBlack.size());
	int nCurPosX = nPosX;
	int nCurPosY = nPosY;
	for(int i = 0; i < nImageCount; ++i)
	{
		m_arrayRectBlack[i]->SetPos(nCurPosX, nCurPosY);
		m_arrayRectNormal[i]->SetPos(nCurPosX, nCurPosY);
		m_arrayRectHighlight[i]->SetPos(nCurPosX, nCurPosY);
		nCurPosX += m_nImageWidth + nMargin;
	}
}

void InputGage::SetCurGage(int nNew)
{
	m_nCurGage = max(m_nMaxGage, nNew);
}

void InputGage::SetMaxGage(int nMax)
{
	m_nCurGage = m_nMaxGage = nMax;
}


